/*
 * \brief  Server-side de-marshaller of ram session interface
 * \author Norman Feske
 * \date   2006-05-10
 */

/*
 * Copyright (C) 2006-2009 Norman Feske
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__RAM_SESSION__SERVER_H_
#define _INCLUDE__RAM_SESSION__SERVER_H_

#include <base/server.h>
#include <ram_session/ram_session.h>

namespace Genode {

	class Ram_session_server : public Server_object, public Ram_session
	{
		public:

			int dispatch(int op, Ipc_istream &is, Ipc_ostream &os)
			{
				switch (op) {

				case ALLOC:
					{
						Ram_dataspace_capability ds;
						size_t                   size = 0;

						is >> size;
						try { ds = alloc(size); }
						catch (Quota_exceeded) {
							os << Ram_dataspace_capability();
							return -1;
						}
						os << ds;
						return 0;
					}

				case FREE:
					{
						Ram_dataspace_capability ds;

						is >> ds;
						free(ds);
						return 0;
					}

				case REF_ACCOUNT:
					{
						Ram_session_capability ram_session;

						is >> ram_session;
						return ref_account(ram_session);
					}

				case TRANSFER_QUOTA:
					{
						Ram_session_capability ram_session;
						size_t amount = 0;

						is >> ram_session >> amount;
						return transfer_quota(ram_session, amount);
					}

				case QUOTA:

					os << quota();
					return 0;

				case USED:

					os << used();
					return 0;
				}
				return -1;
			}
	};
}

#endif /* _INCLUDE__RAM_SESSION__SERVER_H_ */
